package com.hyp.controller;

import com.hyp.pojo.PageBean;
import com.hyp.pojo.Product;
import com.hyp.pojo.Result;
import com.hyp.service.ProductService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j //显示日志消息
@RestController
public class ProductController {
    @Autowired  //注入service层
    private ProductService productService;

    //查询
        //查询全部商品表
//    @RequestMapping("/productList")
//    public Result productList(){
//        log.info("查询全部商品表");
//        List<Product> productList = productService.list();
//        return Result.success(productList);
//    }

    @GetMapping("/allCategories")
    public Result allCategories() {
        List<String> categories = productService.getAllCategories();
        return Result.success(categories);
    }
        //分页查询和条件分页查询
    @GetMapping("/productList")    //@RequestParam的defaultValue属性可以设置参数的默认值
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String category,String description,Integer low,Integer high){
        PageBean pageBean = productService.page(page,pageSize,category,description,low,high);
        return Result.success(pageBean);
    }

    //修改商品信息
         //查询回显,根据商品ID进行查询
    @GetMapping("/productList/{id}")
    public Result getById(@PathVariable Integer id){//@PathVariable获取路径ID
        Product product= productService.getById(id);
        return Result.success(product);
    }
        //更新商品信息
    @PutMapping("/productList")
    public Result update(@RequestBody Product product){
        productService.update(product);
        return Result.success();
    }

    //增加商品
    @PostMapping("/productList")
    public Result addSave(@RequestBody Product product){
        productService.addSave(product);
        return Result.success(product);
    }

    //删除商品
        //可批量删除可单个删除
    @DeleteMapping("/productList/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        productService.delete(ids);
        return Result.success();
    }

    @GetMapping("/catelist/{category}")
    public Result catelist(@PathVariable String category) {
        System.out.println("类别是:"+category);
        List<Product> catelist = productService.catelist(category);
        return Result.success(catelist);
    }
}
